home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / RZToDoList / Source / lock_file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  2.1 KB  |  92 lines

  1. /* 
  2.  * lock_file - functions for file locking
  3.  *
  4.  * You may freely copy, distribute and reuse the code in this example.
  5.  * This code is provided AS IS without warranty of any kind, expressed 
  6.  * or implied, as to its fitness for any particular use.
  7.  *
  8.  * Copyright 1995 Ralph Zazula (rzazula@next.com).  All Rights Reserved.
  9.  *
  10.  */
  11.  
  12. #include "lock_file.h"
  13. #include <libc.h>
  14. #include <stdio.h>
  15. #include <errno.h>
  16.  
  17. int lock_file(char *filename)
  18. /*
  19.  * Drops a lock file "filename.lock" for "filename".  Returns:
  20.  *     LOCK_SUCCESS    (0)    success
  21.  *     LOCK_EXIST        (-1)    lock file already exists
  22.  *     LOCK_NOCREATE    (-2)    couldn't create lock file
  23.  */
  24. {
  25.     int fd;
  26.     char lockfile[MAXPATHLEN+1];
  27.  
  28.     sprintf(lockfile, "%s.lock", filename);
  29.     fd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0660);
  30.     if(fd < 0) {
  31.         if(errno == EEXIST) {
  32.             /* check age of lock file */
  33.             struct stat st;
  34.             if(!stat(lockfile, &st)) {
  35.                 if((time(NULL) - st.st_mtime) >= 3600) {
  36. #ifdef DEBUG
  37.                     fprintf(stderr, "removing stale lock file for %s\n",filename);
  38. #endif
  39.                     unlink(lockfile);
  40.                     fd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0660);
  41.                     if(fd < 0) {
  42.                         if(errno == EEXIST) {
  43. #ifdef DEBUG
  44.                             fprintf(stderr, "couldn't remove lock file for %s\n",filename);
  45. #endif
  46.                         } else {
  47. #ifdef DEBUG
  48.                             fprintf(stderr, "couldn't create lock file for %s\n",filename);                            
  49. #endif
  50.                         }
  51.                         return LOCK_NOCREATE;
  52.                     } else {
  53.                         close(fd);
  54.                         return LOCK_SUCCESS;
  55.                     }
  56.                 } else {
  57.                     return LOCK_EXIST;
  58.                 }
  59.             } else {
  60. #ifdef DEBUG
  61.                 fprintf(stderr, "couldn't stat %s\n",lockfile);
  62. #endif
  63.                 return LOCK_EXIST;
  64.             }
  65.         } else {
  66. #ifdef DEBUG
  67.             fprintf(stderr, "cannot create lock file %s\n", lockfile);
  68. #endif
  69.             return LOCK_NOCREATE;
  70.         }
  71.     } else {
  72.         close(fd);
  73.     }
  74.     return LOCK_SUCCESS;
  75. }
  76.  
  77. int unlock_file(char *filename)
  78. /*
  79.  * removes the lock file "filename.lock" for "filename".
  80.  * Returns LOCK_SUCCESS on success and a negative error code on failure.
  81.  * The error code is that which resulted from the attempted unlink().
  82.  */
  83. {
  84.     char lockfile[MAXPATHLEN+1];
  85.     
  86.     sprintf(lockfile, "%s.lock", filename);
  87.     if(!unlink(lockfile)) {
  88.         return LOCK_SUCCESS;
  89.     }
  90.     return errno;
  91. }
  92.